package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import service.Service;
import javax.swing.SwingConstants;
import java.awt.Color;

/**
 * 
 * @author Martin Sax og Daniel Nissen
 * 
 */
public class RegistrereMellemVare extends JPanel implements CarlettiPanel {
	private JLabel lblRegistrereNyMellemVare;
	private JButton RegVare;
	private JLabel lblIndtastVareNr;
	private JTextField textField;
	private JPanel panel_2;
	private JLabel lblAnvistVareNr;
	private JLabel lblXx;
	private JPanel panel;
	private JPanel panel_3;
	private JButton btnRegNyVare;
	private JScrollPane scrollPane;
	private JList list;
	private JLabel lblAnvistPladsNr_1;
	private JPanel panel_4;
	private JLabel lblXx_1;
	private JPanel panel_5;
	private JSeparator separator_1;

	/**
	 * Create the panel.
	 */
	public RegistrereMellemVare() {
		setLayout(new BorderLayout(0, 0));

		panel_3 = new JPanel();
		add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new BorderLayout(0, 0));

		textField = new JTextField();
		panel_3.add(textField, BorderLayout.CENTER);
		textField.setFont(new Font("Tahoma", Font.PLAIN, 20));
		textField.setColumns(10);

		lblIndtastVareNr = new JLabel("Indtast Vare Nr.");
		panel_3.add(lblIndtastVareNr, BorderLayout.NORTH);
		lblIndtastVareNr.setFont(new Font("Tahoma", Font.PLAIN, 22));

		RegVare = new JButton("Reg. Vare");
		panel_3.add(RegVare, BorderLayout.EAST);
		RegVare.setMargin(new Insets(40, 24, 40, 24));

		panel_5 = new JPanel();
		panel_3.add(panel_5, BorderLayout.SOUTH);
		RegVare.addActionListener(new BtnNewButtonActionListener());

		panel_2 = new JPanel();
		add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		panel_2.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		btnRegNyVare = new JButton("Reg. Ny Vare");
		btnRegNyVare.addActionListener(new BtnRegNyVareActionListener());
		btnRegNyVare.setMargin(new Insets(40, 24, 40, 24));
		panel.add(btnRegNyVare, BorderLayout.EAST);

		scrollPane = new JScrollPane();
		panel.add(scrollPane);

		list = new JList();
		list.setVisibleRowCount(3);
		list.setFont(new Font("Tahoma", Font.PLAIN, 20));
		updateList();
		scrollPane.setViewportView(list);

		panel_4 = new JPanel();
		panel.add(panel_4, BorderLayout.SOUTH);

		lblAnvistPladsNr_1 = new JLabel("Sidste anvist plads nr.");
		lblAnvistPladsNr_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_4.add(lblAnvistPladsNr_1);

		lblXx_1 = new JLabel("XX");
		lblXx_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_4.add(lblXx_1);

		separator_1 = new JSeparator();
		separator_1.setForeground(Color.BLACK);
		separator_1.setOrientation(SwingConstants.VERTICAL);
		panel_4.add(separator_1);

		lblAnvistVareNr = new JLabel("Sidste anviste vare nr:");
		panel_4.add(lblAnvistVareNr);
		lblAnvistVareNr.setFont(new Font("Tahoma", Font.PLAIN, 20));

		lblXx = new JLabel("XX");
		panel_4.add(lblXx);
		lblXx.setFont(new Font("Tahoma", Font.PLAIN, 20));

		lblRegistrereNyMellemVare = new JLabel("Vælge Produkt Type.");
		panel_2.add(lblRegistrereNyMellemVare, BorderLayout.NORTH);
		lblRegistrereNyMellemVare.setFont(new Font("Tahoma", Font.PLAIN, 20));

		JSeparator separator = new JSeparator();
		add(separator, BorderLayout.CENTER);

	}

	public JList getList() {
		return list;
	}

	public void updateList() {
		list.setListData(Service.getAllProduktType().toArray());

	}

	private class BtnNewButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (textField.getText().equals("")) {
				JOptionPane.showMessageDialog(RegistrereMellemVare.this,
						"Du mangler at indtaste et vare nummer", "Fejl",
						JOptionPane.ERROR_MESSAGE);
			} else {
				int i = 0;
				boolean found = false;

				try {
					while (i < Service.getAllMellemVare().size() && !found) {
						if (Service.getAllMellemVare().get(i).getVareNummer() == Integer
								.parseInt(textField.getText())) {
							found = true;
							Service.timeStampMellemVare(Service
									.getAllMellemVare().get(i));
						} else {
							i++;
						}
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(RegistrereMellemVare.this,
							e.getMessage(), "Fejl", JOptionPane.ERROR_MESSAGE);
				}

				if (!found) {
					JOptionPane.showMessageDialog(RegistrereMellemVare.this,
							"Dette vare nummer eksistere ikke", "Fejl",
							JOptionPane.ERROR_MESSAGE);
				}
			}
			textField.setText("");
		}
	}

	private class BtnRegNyVareActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (Service.getLager().getFriPlads() != null) {
					JOptionPane.showMessageDialog(RegistrereMellemVare.this,
							"Placere din vare på placering nr: "
									+ Service.getLager().getFriPlads()
											.getPladsNr(), "Plads Anvisning",
							JOptionPane.INFORMATION_MESSAGE);
				}

				lblXx_1.setText(""
						+ Service.getLager().getFriPlads().getPladsNr());
				Service.registrerMellemvare(((model.ProduktType) list
						.getSelectedValue()));
				lblXx.setText("" + Service.getAllMellemVare().get(Service.getAllMellemVare().size()-1).getVareNummer());
			} catch (Exception e) {
				JOptionPane.showMessageDialog(RegistrereMellemVare.this,
						"Du har ikke valgt en produkt type" + e, "Fejl",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	public void update() {
		updateList();

	}
}
